<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/style.css" />
    <script type="text/javascript" src="../../lib/aircity/ac_conf.js"></script>
    <script type="text/javascript" src="../../lib/aircity/ac.min.js"></script>
    <script type="text/javascript" src="../../lib/vue3.js"></script>
  </head>
  <body>
    <div id="app">
      <div id="player" style="height: 100%"></div>
      <div class="control">
        <div class="top">
          <div>等高线分析</div>
        </div>
        <div class="params">
          <div class="title">参数设置</div>
          <div class="item">
            <div class="label">显示样式</div>
            <div class="content">
              <div class="flex-box">
                <input type="checkbox" name="" v-model="options.isShowContourPlane" />
                <span style="margin: 0px 5px">等高面</span>
              </div>
              <div class="flex-box">
                <input type="checkbox" name="" v-model="options.isShowContourLine" />
                <span style="margin: 0px 5px">等高线</span>
              </div>
            </div>
          </div>

          <div class="item">
            <div class="label">不透明度</div>
            <div class="content">
              <input type="number" v-model="options.contourPlaneOpacity" :step="0.1" :min="0" :max="1" />
            </div>
          </div>

          <div class="item">
            <div class="label">最大可视高度</div>
            <div class="content">
              <input type="number" v-model="options.contourLineRangeMax" />
              m
            </div>
          </div>
          <div class="item">
            <div class="label">最小可视高度</div>
            <div class="content">
              <input type="number" v-model="options.contourLineRangeMin" />
              m
            </div>
          </div>

          <div class="item">
            <div class="label">线颜色</div>
            <div class="content">
              <input type="color" v-model="options.contourLineColor" />
            </div>
          </div>
          <div class="item">
            <div class="label">线间距</div>
            <div class="content">
              <input type="number" v-model="options.contourLineSpacing" />
              m
            </div>
          </div>

          <div class="button-box">
            <button @click="start()">开始等高线分析</button>
          </div>

          <div class="button-box">
            <button @click="stop()" class="sunlight stop">停止等高线分析</button>
          </div>
        </div>
      </div>
    </div>

    <script type="text/javascript">
      const { createApp, reactive, watch } = Vue

      const vueComposition = {
        setup() {
          // 等高线分析 代码块
          const options = reactive({
            isShowContourPlane: true, //是否显示等高面，{boolean}，默认值：true
            contourPlaneOpacity: 1, //等高面颜色不透明度，仅当isShowContourPlane设置为true时生效，{number}，取值范围：[0~1]，默认值：1
            isShowContourLine: true, //是否显示等高线，{boolean}，默认值：true
            contourLineColor: '#ffffff', //等高线颜色值，仅当isShowContourLine设置为true时生效，{Color}，默认值：[1,1,1,1]
            contourLineSpacing: 20, //等高线的间距，仅当isShowContourLine设置为true时生效，{number}，单位：米，默认值：20米
            contourLineRangeMax: 1000000, //等高线最大可视高度，{number}，单位：米，默认值：1000000米
            contourLineRangeMin: -1000000 //等高线最小可视高度，{number}，单位：米，默认值：-1000000米
          })

          // 分析状态，true为正在分析，false则没有进行分析
          let analysisState = false

          // 开始等高线分析
          const start = () => {
            // 设置分析状态为 true
            analysisState = true
            const o = JSON.parse(JSON.stringify(options))
            o.contourLineColor = toColorArr(o.contourLineColor)
            // 开启分析
            __g.tools.startContourLineAnalysis(o)
          }

          // 防抖
          const debounce = (func, wait) => {
            let timeout = null
            return function () {
              if (analysisState) {
                clearTimeout(timeout)
                timeout = setTimeout(() => {
                  func()
                }, wait)
              }
            }
          }
          // options监听，执行防抖的 开启分析事件
          watch(options, debounce(start, 500), {
            deep: true
          })

          // 停止等高线分析
          const stop = () => {
            // 设置分析状态为 false
            analysisState = false
            // 关闭分析
            __g.tools.stopContourLineAnalysis()
          }

          const toColorArr = (sColor, opacity = 1) => {
            const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
            if (sColor && reg.test(sColor)) {
              if (sColor.length === 4) {
                var sColorNew = '#'
                for (var i = 1; i < 4; i += 1) {
                  sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
                }
                sColor = sColorNew
              }
              //处理六位的颜色值
              var sColorChange = []
              for (var i = 1; i < 7; i += 2) {
                sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)) / 255)
              }
              sColorChange.push(opacity)
              return sColorChange
            } else {
              return sColor
            }
          }

          return {
            options,
            start,
            stop
          }
        }
      }

      const app = createApp(vueComposition).mount('#app')

      const OnLoad = () => {
        const __DigitalTwinPlayer = new DigitalTwinPlayer(HostConfig.Player, { domId: 'player' })
      }

      //自适应分辨率
      const onResize = async () => {
        let playerView = document.getElementById('player')
        playerView.style.height = window.innerHeight + 'px'
        playerView.style.width = window.innerWidth + 'px'
      }

      window.addEventListener('load', OnLoad, true)
      window.addEventListener('resize', onResize, true)

      //页面离开事件(离开时清除此页面的操作及重置场景)
      window.onunload = function (e) {
        __g.tools.stopContourLineAnalysis()
        // 重置场景
        __g.reset()
      }
    </script>
  </body>
</html>
